//
//  Home.swift
//  CustomCarousel
//
//  Created by 云控蜂核 on 2023/5/4.
//

import SwiftUI

struct Home: View {
    
    @State var selectedTrip = trips[0]
    
    
    init() {
        UIScrollView.appearance().bounces = false
    }
    
    var body: some View {
        ZStack{
            
            GeometryReader { proxy in
                let frame = proxy.frame(in: .global)
                
                Image(selectedTrip.image)
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: frame.width, height: frame.height, alignment:.center)
                //why we using corner radius
                //because image in swiftUI using .fill will require corner radius or clipshape to cut the image...
                    .cornerRadius(0)
            }
            .ignoresSafeArea()
            
            VStack{
                Text("Let's go with")
                    .fontWeight(.bold)
                    .foregroundColor(.white)
                
                Text("PocoTrip")
                    .font(.largeTitle)
                    .fontWeight(.heavy)
                    .foregroundColor(.white)
                    .padding(.bottom, 30)
                
                VStack {
                    GeometryReader{ proxy in
                        let frame = proxy.frame(in: .global)
                        
                        TabView(selection: $selectedTrip) {
                            ForEach(trips) { trip in
                                Image(trip.image)
                                    .resizable()
                                    .aspectRatio(contentMode: .fill)
                                    .frame(width: frame.width - 10, height: frame.height, alignment: .center)
                                    .cornerRadius(4)
                                    .tag(trip)
                                
                                
                            }
                        }
                        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))

                    }
                    .frame(height: UIScreen.main.bounds.height / 2.2)
                    
                    Text(selectedTrip.title)
                        .font(.title)
                        .fontWeight(.bold)
                        .padding(.top)
                        .padding(.bottom, 18)
                    
                    PageControl(maxPages: trips.count, currentPage: getIndex())
                    
                }
                .padding(.top)
                .padding(.horizontal, 10)
                .padding(.bottom, 5)
                
                .background(Color.white.clipShape(CustomShape()).cornerRadius(10))
                .padding(.horizontal, 20)
                
                Button {
                    
                } label: {
                    Text("GET STARTING")
                        .fontWeight(.bold)
                        .foregroundColor(.white)
                        .padding(.vertical, 18)
                        .frame(maxWidth: .infinity)
                        .background(Color("Blue"))
                        .cornerRadius(10)
                }
                .padding(.top, 30)
                .padding(.horizontal)
            }
            .padding()
            
        }
        
        
    }
    
    func getIndex() -> Int {
        let index = trips.firstIndex { trip in
            return selectedTrip.id == trip.id
        } ?? 0
        return index
    }
    
}

struct Home_Previews: PreviewProvider {
    static var previews: some View {
        Home()
    }
}
